package com.ztp.pc.gui;

import static com.ztp.lib.server.UsersTableShema.DOWNRATE_C;
import static com.ztp.lib.server.UsersTableShema.MAXLOGINPERIP_C;
import static com.ztp.lib.server.UsersTableShema.MAXLOGIN_C;
import static com.ztp.lib.server.UsersTableShema.UPRATE_C;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import org.apache.ftpserver.ftplet.Authentication;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.ftplet.User;
import org.apache.ftpserver.usermanager.PasswordEncryptor;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.ConcurrentLoginPermission;
import org.apache.ftpserver.usermanager.impl.ConcurrentLoginRequest;
import org.apache.ftpserver.usermanager.impl.TransferRatePermission;
import org.apache.ftpserver.usermanager.impl.TransferRateRequest;
import org.apache.ftpserver.usermanager.impl.WritePermission;
import org.apache.ftpserver.usermanager.impl.WriteRequest;

import com.ztp.lib.server.ServerHelper;

import net.miginfocom.swing.MigLayout;

class UserEditPanel extends JPanel {
	
	private ServerHelper mServerHelper;
	
	private JTextField 	nameTF;
	private JTextField 	passTF;
	private JTextField 	homeDirTF;
	private JButton 	homeDirBTN;
	private JCheckBox 	enableCHBX;
	private JCheckBox 	writeAccessCHBX;
	private JSpinner 	idleTimeSPN;
	private JSpinner 	upRateSPN;
	private JSpinner 	downRateSPN;
	private JSpinner 	maxLoginNumberSPN;
	private JSpinner 	maxLoginPerIpSPN;


	UserEditPanel(ServerHelper sh){
		mServerHelper = sh;
		
		this.setLayout(new MigLayout("", "[][][][][171.00,grow][grow]", "[][][][][][][][][][]"));
		
		//panelClients.add(userEditPanel, gbc_panel_1);
		
		JLabel lblNewLabel_1 = new JLabel("User name");
		this.add(lblNewLabel_1, "cell 2 0");
		
		nameTF = new JTextField();
		this.add(nameTF, "cell 4 0,growx");
		nameTF.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("Password");
		this.add(lblNewLabel_2, "cell 2 1");
		
		passTF = new JTextField();
		this.add(passTF, "cell 4 1,growx");
		passTF.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("Home directory");
		this.add(lblNewLabel_3, "cell 2 2");
		
		homeDirBTN = new JButton("...");
		homeDirBTN.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JFileChooser fc = new JFileChooser();
				fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int answer = fc.showOpenDialog(null);
				if(answer == JFileChooser.APPROVE_OPTION){
					homeDirTF.setText(fc.getSelectedFile().getAbsolutePath());
				}
			}
		});
		
		homeDirTF = new JTextField();
		this.add(homeDirTF, "flowx,cell 4 2,growx");
		homeDirTF.setColumns(10);
		homeDirTF.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent arg0) {
				homeDirTF.setToolTipText(homeDirTF.getText());
			}
			
			@Override
			public void insertUpdate(DocumentEvent arg0) {
				homeDirTF.setToolTipText(homeDirTF.getText());
			}
			
			@Override
			public void changedUpdate(DocumentEvent arg0) {
				homeDirTF.setToolTipText(homeDirTF.getText());
			}
		});
		this.add(homeDirBTN, "cell 4 2,alignx right");
		
		JLabel lblNewLabel_4 = new JLabel("Enable");
		this.add(lblNewLabel_4, "cell 2 3");
		
		enableCHBX = new JCheckBox("");
		this.add(enableCHBX, "cell 4 3,alignx right");
		
		JLabel lblNewLabel_5 = new JLabel("Write acces");
		this.add(lblNewLabel_5, "cell 2 4");
		
		writeAccessCHBX = new JCheckBox("");
		this.add(writeAccessCHBX, "cell 4 4,alignx right");
		
		JLabel lblNewLabel_6 = new JLabel("Idle time");
		this.add(lblNewLabel_6, "cell 2 5");
		
		idleTimeSPN = new JSpinner();
		idleTimeSPN.setPreferredSize(new Dimension(80, 18));
		this.add(idleTimeSPN, "cell 4 5,alignx right");
		
		JLabel lblNewLabel_7 = new JLabel("Upload rate");
		this.add(lblNewLabel_7, "cell 2 6");
		
		upRateSPN = new JSpinner();
		upRateSPN.setPreferredSize(new Dimension(80, 18));
		this.add(upRateSPN, "cell 4 6,alignx right");
		
		JLabel lblNewLabel_8 = new JLabel("Download rate");
		this.add(lblNewLabel_8, "cell 2 7");
		
		downRateSPN = new JSpinner();
		downRateSPN.setPreferredSize(new Dimension(80, 18));
		this.add(downRateSPN, "cell 4 7,alignx right");
		
		JLabel lblNewLabel_9 = new JLabel("Max. login number");
		this.add(lblNewLabel_9, "cell 2 8");
		
		maxLoginNumberSPN = new JSpinner();
		maxLoginNumberSPN.setPreferredSize(new Dimension(80, 18));
		this.add(maxLoginNumberSPN, "cell 4 8,alignx right");
		
		JLabel lblNewLabel_10 = new JLabel("Max. login per IP");
		this.add(lblNewLabel_10, "cell 2 9");
		
		maxLoginPerIpSPN = new JSpinner();
		maxLoginPerIpSPN.setPreferredSize(new Dimension(80, 18));
		this.add(maxLoginPerIpSPN, "cell 4 9,alignx right");
	}
	
	public void setDataForEditing(User user){
		
		nameTF.setEnabled(false);
		
		nameTF.setText(user.getName());
		System.out.println("user pass " + user.getPassword());
		passTF.setText(user.getPassword());
		homeDirTF.setText(user.getHomeDirectory());
		enableCHBX.setSelected(user.getEnabled());
		writeAccessCHBX.setSelected( user.authorize(new WriteRequest()) != null);
		idleTimeSPN.setValue(user.getMaxIdleTime());
		
		TransferRateRequest trr = new TransferRateRequest();
		trr = (TransferRateRequest) user.authorize(trr);
		if(trr != null){
			upRateSPN.setValue(trr.getMaxUploadRate());
			downRateSPN.setValue(trr.getMaxDownloadRate());
		}
		else{
			upRateSPN.setValue(0);
			downRateSPN.setValue(0);
		}
		
		ConcurrentLoginRequest clr = new ConcurrentLoginRequest(0, 0);
		clr = (ConcurrentLoginRequest) user.authorize(clr);
		if(clr != null){
			maxLoginNumberSPN.setValue(clr.getMaxConcurrentLogins());
			maxLoginPerIpSPN.setValue(clr.getMaxConcurrentLoginsPerIP());
		}
		else{
			maxLoginNumberSPN.setValue(0);
			maxLoginPerIpSPN.setValue(0);
		}
	}
	
	
	
	public void clearPanel(){
		nameTF.setEnabled(true);
		nameTF.setText("");
		passTF.setText("");
		homeDirTF.setText("");
		enableCHBX.setSelected(true);
		writeAccessCHBX.setSelected(false);
		idleTimeSPN.setValue(0);
		upRateSPN.setValue(0);
		downRateSPN.setValue(0);
		maxLoginNumberSPN.setValue(0);
		maxLoginPerIpSPN.setValue(0);
	}
	
	public User getEditedUser(){
		BaseUser user = new BaseUser();
		user.setName(nameTF.getText());
		user.setPassword(mServerHelper.pw.encrypt(passTF.getText()));
		user.setHomeDirectory(homeDirTF.getText());
		user.setEnabled(enableCHBX.isSelected());
		user.setMaxIdleTime((Integer)idleTimeSPN.getValue());
		
		ArrayList<Authority> authorities = new ArrayList<Authority>();
		if(writeAccessCHBX.isSelected()){
			authorities.add(new WritePermission());
		}
		
		authorities.add( new TransferRatePermission(
				(Integer)downRateSPN.getValue()
				,(Integer) upRateSPN.getValue()));
		
		authorities.add(new ConcurrentLoginPermission(
				(Integer) maxLoginNumberSPN.getValue()
				,(Integer) maxLoginPerIpSPN.getValue()));
		
		user.setAuthorities(authorities);
		return user;
	}
	
	
}
